package com.test.mylist;

import java.util.ArrayList;
import java.util.List;


import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;

public class MyListActivity extends ListActivity {
	Button addBtn, remBtn;
	/** Called when the activity is first created. */
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);  // Still don't know where icicle is coming from
		// Create an array of Strings, that will be put to our ListActivity
		setContentView(R.layout.main);  // Set the layout for the screen
		final ArrayAdapter<Model> adapter = new InteractiveArrayAdapter(this,getModel()); // Fill the adapter
		setListAdapter(adapter);  // Send the adapter to the ListView -- Appears to update in real time as long as adapter is the edit target.
		
		addBtn = (Button) findViewById(R.id.btnAdd);  // Bind add button
		remBtn = (Button) findViewById(R.id.btnRemove);  // Bind remove button
		
		// Add button - proc new activity to take information on new object
		addBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Implement Adding Function (Replace the current model scanner)
				String outputStr = "";
				for(int i =0; i<adapter.getCount(); i++)
				{					
					if(adapter.getItem(i).isSelected())
					{
						outputStr = outputStr + i +" ";
					}
				}
				Log.d("MODEL Results","Selected:"+outputStr);
			}
		});
		
		
		// Delete button - remove all checked 
		remBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				for(int i =0; i<adapter.getCount(); i++) // Scan the adapter list
				{
					if(adapter.getItem(i).isSelected()) // If the item is checked
					{
						adapter.remove(adapter.getItem(i));  // Remove the item
						i--;  // Decrement the position b/c the item was removed, forcing everthing else to shift.
					}
				}
			}
		});
		
	}

	/**
	 * Should be replace with data from DB
	 * @return
	 */
	private List<Model> getModel() { 
		List<Model> list = new ArrayList<Model>();
		list.add(get("0"));
		list.add(get("1"));
		list.add(get("2"));
		list.add(get("3"));
		list.add(get("4"));
		list.add(get("5"));
		list.add(get("6"));
		list.add(get("7"));
		list.add(get("8"));
		list.add(get("9"));
		list.add(get("10"));
		list.add(get("11"));
		list.add(get("12"));
		list.add(get("13"));
		list.add(get("14"));
		list.add(get("15"));
		list.add(get("16"));
		list.add(get("17"));
		list.add(get("18"));
		list.add(get("19"));
		list.add(get("20"));
		// Initially select one of the items
		list.get(1).setSelected(true);
		return list;
	}

	/**
	 * Create the Model object to add to the list
	 * @param s  -  String for the name
	 * @param d  -  String for the description
	 * @return
	 */
	private Model get(String s, String d) {
		return new Model(s,d);
	}
	
	private Model get(String s) {
		return new Model(s);
	}

}